﻿package mortal.game.control
{
    import Message.DB.Tables.*;
    import Message.Public.*;
    import flash.events.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.view.Title.*;
    import mortal.game.view.palyer.data.*;
    import mortal.game.view.systemSetting.*;
    import mortal.game.view.tianDiRank.data.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class TitleController extends Controller
    {
        private var _titleModule:TitleModule;

        public function TitleController()
        {
            return;
        }// end function

        override protected function initView() : IView
        {
            if (this._titleModule == null)
            {
                this._titleModule = new TitleModule();
                this._titleModule.addEventListener(WindowEvent.SHOW, this.onTitleWinShow);
                this._titleModule.addEventListener(WindowEvent.CLOSE, this.onTitleWinClose);
            }
            return this._titleModule;
        }// end function

        override protected function initServer() : void
        {
            NetDispatcher.addCmdListener(ServerCommand.TitleGetAllTitles, this.getAllTitleHandler);
            NetDispatcher.addCmdListener(ServerCommand.TitleUpdateTitle, this.updateTitleHandler);
            NetDispatcher.addCmdListener(ServerCommand.SystemClientSettingGot, this.updateTitleHideStatus);
            NetDispatcher.addCmdListener(ServerCommand.TitleResetHideStatus, this.resetTitleHideStatus);
            NetDispatcher.addCmdListener(ServerCommand.RoleLevelUpdate, this.onLevelUpdateHandler);
            Dispatcher.addEventListener(EventName.TitleItemUse, this.activeTitleHandler);
            return;
        }// end function

        private function onTitleWinShow(event:Event) : void
        {
            return;
        }// end function

        private function onTitleWinClose(event:Event) : void
        {
            return;
        }// end function

        private function getAllTitleHandler(param1:Object = null) : void
        {
            cache.title.initTitleData(param1.titles);
            if (this._titleModule && !this._titleModule.isHide)
            {
                this._titleModule.updateAll();
            }
            return;
        }// end function

        private function onLevelUpdateHandler(param1:Object) : void
        {
            var _loc_2:* = cache.title.sSeqTitle;
            this.getAllTitleHandler(_loc_2);
            return;
        }// end function

        private function updateTitleHandler(param1:Object) : void
        {
            var _loc_2:* = param1 as SPlayerTitle;
            var _loc_3:* = _loc_2.updateType;
            var _loc_4:* = _loc_2.title;
            var _loc_5:* = TitleConfig.instance.getInfoById(_loc_4.titleCode);
            var _loc_6:* = new TitleDataInfo();
            _loc_6.titleData = _loc_5;
            _loc_6.titleStatus = _loc_4.status;
            _loc_6.exStr = _loc_4.str;
            switch(_loc_3.value())
            {
                case EUpdateType._EUpdateTypeAdd:
                {
                    Cache.instance.title.updateTitleRecord(_loc_6, _loc_3.value());
                    break;
                }
                case EUpdateType._EUpdateTypeDel:
                {
                    Cache.instance.title.updateTitleRecord(_loc_6, _loc_3.value());
                    break;
                }
                case EUpdateType._EUpdateTypeUpdate:
                {
                    Cache.instance.title.updateTitleData(_loc_6);
                    break;
                }
                default:
                {
                    break;
                }
            }
            if (this._titleModule && !this._titleModule.isHide)
            {
                this._titleModule.updateAll();
            }
            var _loc_7:* = Cache.instance.title.titleUpdateType;
            if (_loc_7 == EUpdateType._EUpdateTypeAdd && TianDiRankUtil.isTianDianTitle(_loc_6) == false)
            {
                TitleUtil.showTitleTips(_loc_6, _loc_7);
            }
            return;
        }// end function

        private function updateTitleHideStatus(param1:Object) : void
        {
            if (ClientSetting.local.getIsDone(IsDoneType.HideTitle))
            {
                GameProxy.titleProxy.hideTitle(0);
            }
            return;
        }// end function

        private function resetTitleHideStatus(param1:Object) : void
        {
            if (ClientSetting.local.getIsDone(IsDoneType.HideTitle))
            {
                ClientSetting.local.setIsDone(false, IsDoneType.HideTitle);
            }
            if (this._titleModule && !this._titleModule.isHide)
            {
                this._titleModule.resetHideStatus();
            }
            return;
        }// end function

        private function activeTitleHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as ItemData;
            GameProxy.titleProxy.activeTitle(_loc_2.serverData.uid);
            return;
        }// end function

    }
}
